package com.baosight.risk.web.rire;

import com.baosight.risk.api.rire.RireRiskWarningStatusService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.rire.entity.RireRiskEvent;
import com.baosight.risk.service.rire.entity.RireRiskTarget;
import com.baosight.risk.service.rire.entity.RireRiskWarningStatus;
import com.baosight.risk.service.rire.vo.RiskWarningStatusVO;
import com.baosight.risk.service.riwa.entity.RiwaRiskWaringRule;
import com.baosight.risk.service.riwa.entity.RiwaRiskWaringTarget;
import com.baosight.risk.service.riwa.vo.RiwaRiskWaringVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author phinxe
 * @Date 2019-12-11
 */
@Slf4j
@RestController
@RequestMapping("/warning/report")
@Api(value = "重点风险披露报告", description = "重点风险==披露报告", produces = MediaType.APPLICATION_JSON)
public class RireRiskWarningStatusController extends BaseController {
    @Autowired
    private RireRiskWarningStatusService service;

    @GetMapping("/getWarningReport")
    @ApiOperation("根据ID获取披露报告")
    public AjaxResult getWarningById(String id) throws Exception {
        Map<String, Object> result = service.getReportById(id);
        if (result.isEmpty()) {
            return error("获取披露报告失败");
        }
        return success(result);
    }

    @GetMapping("/getReportTarget")
    @ApiOperation("根据披露报告ID获取披露报告指标")
    public AjaxResult getReportTarget(String id) {
        // 获取披露报告指标信息(列表)
        List<RireRiskTarget> targetList = service.getTargetList(id);
        return success(targetList);
    }

    @GetMapping("/getReportEvent")
    @ApiOperation("根据披露报告ID获取披露报告风险事件")
    public AjaxResult getReportEvent(String id) {
        // 获取披露报告事件信息(列表)
        List<RireRiskEvent> eventList = service.getEventList(id);
        return success(eventList);
    }

    @GetMapping("/getReportCommonModel")
    @ApiOperation("根据披露报告ID获取披露报告普通模型")
    public AjaxResult getReportCommonModel(String id) {
        return success(service.getModel(id, true));
    }

    @PostMapping("/saveWarning")
    @ApiOperation("保存修改披露报告")
    public AjaxResult saveWarning(@RequestBody RireRiskWarningStatus report) {
        if (service.updateWarning(report)) {
            return success();
        }
        return error("保存披露报告失败！");
    }

    @PostMapping("/saveTarget")
    @ApiOperation("保存披露报告风险指标")
    public AjaxResult saveTarget(@RequestBody List<RireRiskTarget> targets) {
        List<RireRiskTarget> result = service.updateTarget(targets);
        if (result.size() > 0) {
            return success(result);
        }
        return error("保存披露报告风险指标失败！");
    }

    @PostMapping("/saveEvent")
    @ApiOperation("保存披露报告风险事件")
    public AjaxResult saveEvent(@RequestBody List<RireRiskEvent> events) {
        List<RireRiskEvent> result = service.updateEvent(events);
        if (result.size() > 0) {
            return success(result);
        }
        return error("保存披露报告风险事件失败！");
    }


    @PostMapping("/saveModel")
    @ApiOperation("保存披露报告模型")
    public AjaxResult saveModel(@RequestBody RiwaRiskWaringVO modelVO) {
        Object model = service.updateModel(modelVO);
        if (model != null) {
           /* if(model=="f"){
                return error("同年度已经有披露报告模型，修改失败！");
            }*/
            return success(model);
        }
        return error("保存披露报告模型失败！");
    }

    @PostMapping("/deleteTarget")
    @ApiOperation("删除披露报告风险指标")
    public AjaxResult deleteTarget(@RequestBody List<RireRiskTarget> targets) {
        Boolean result = service.deleteTarget(targets);
        if (result) {
            return success();
        }
        return error("删除披露报告风险指标失败！");
    }

    @PostMapping("/deleteEvent")
    @ApiOperation("删除披露报告风险事件")
    public AjaxResult deleteEvent(@RequestBody List<RireRiskEvent> events) {
        Boolean result = service.deleteEvent(events);
        if (result) {
            return success();
        }
        return error("删除披露报告风险事件失败！");
    }

    @PostMapping("/deleteModelRule")
    @ApiOperation("删除披露报告模型规则")
    public AjaxResult deleteModelRule(@RequestBody List<RiwaRiskWaringRule> rules) {
        Boolean result = service.deleteModelRule(rules);
        if (result) {
            // 获取披露报告模型信息(实体)
            Object model = service.getModel(rules.get(0).getImportGuid(), false);
            return success(model);
        }
        return error("删除披露报告模型规则失败！");
    }

    @PostMapping("/deleteModelTarget")
    @ApiOperation("删除披露报告模型指标")
    public AjaxResult deleteModelTarget(@RequestBody List<RiwaRiskWaringTarget> targets) {
        Boolean result = service.deleteModelTarget(targets);
        if (result) {
            // 获取披露报告模型信息(实体)
            Object model = service.getModel(targets.get(0).getImportGuid(), false);
            return success(model);
        }
        return error("删除披露报告模型指标失败！");
    }

    @PostMapping("/changeCompFlag")
    @ApiOperation("删除披露报告模型指标")
    public AjaxResult changeCompFlag(@RequestBody RireRiskTarget target) {
        if (target != null && StringUtils.isNotEmpty(target.getCompFlag())) {
            RireRiskTarget param = (RireRiskTarget) BeanUtils.updateBean(new RireRiskTarget());
            param.setId(target.getId());
            param.setCompFlag(target.getCompFlag());
            if(service.updateCompFlag(param) < 1) {
                return error();
            }
            return success();
        } else {
            return error();
        }
    }

    @GetMapping("/pushWaring")
    @ApiOperation("推送披露报告")
    public AjaxResult pushWaring(String warningStatusId) {
        Map returnMap = service.pushWarningStatusData(warningStatusId);
        if ("1".equals(returnMap.get("status"))) {
            return success();
        }
        return error(returnMap.get("msg").toString());
    }

}
